package com.zprmb.udzt;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.ll.BaseHelper;
import com.zprmb.udzt.util.BitmapUtils;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.LogUtils;
import com.zprmb.udzt.util.TimeCount;
import com.zprmb.udzt.util.VolleryUtil;

public class Reset_pwd_Mes extends BaseActivity implements OnClickListener {
	private EditText et_phone, et_codes;
	private Button btn_confirm, btn_send_mes;
	private ImageView img_back;
	private TimeCount count;
	private RelativeLayout rl_back;
	private TextView tv_customer;
	private TextView tv_voice_code;
//	private ImageView img_reg_bg;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.reset_pwd_mes);
		BbtApplication.getInstance().addActvity(this);
		et_phone = (EditText) findViewById(R.id.et_phone);
		et_codes = (EditText) findViewById(R.id.et_codes);
		btn_confirm = (Button) findViewById(R.id.btn_confirm);
		btn_send_mes = (Button) findViewById(R.id.btn_send_mes);
		img_back = (ImageView) findViewById(R.id.img_back);	
		rl_back = (RelativeLayout) findViewById(R.id.rl_back);
		tv_customer = (TextView) findViewById(R.id.enterprise_tel);
		tv_voice_code=(TextView)findViewById(R.id.tv_voice_code);
//		img_reg_bg=(ImageView)findViewById(R.id.img_reg_bg);
		tv_voice_code.setText(Html.fromHtml("收不到短信？点击获取"+"<font color='#00aaee'>语音验证码</font>"));
		count = new TimeCount(60000, 100,btn_send_mes,tv_voice_code);
//		img_reg_bg.setImageBitmap(BitmapUtils.readBitMap(this, R.drawable.v6_login_bg));
		tv_voice_code.setOnClickListener(this);
		tv_customer.setOnClickListener(this);
		rl_back.setOnClickListener(this);
		btn_send_mes.setOnClickListener(this);
		btn_confirm.setOnClickListener(this);
		img_back.setOnClickListener(this);
		et_codes.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
            
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
//				if (et_codes.getText().toString().length() == 6) {
//					btn_confirm.setBackgroundResource(R.drawable.v6button);
//					btn_confirm.setEnabled(true);
//				} else {
//					btn_confirm.setBackgroundResource(R.drawable.btn_unclick);
//					btn_confirm.setEnabled(false);
//				}
			}
		});
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		BbtApplication.getInstance().removeActvity(this);
	}

	private void initmes(final int approach) {
		// TODO Auto-generated method stub
		AjaxParams params=new AjaxParams();
		params.put("approach", approach+"");
		params.put("userMobileText", et_phone.getText().toString());
		VolleryUtil volleryUtil = new VolleryUtil() {

			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				if (jsonObject.optInt("success") == 1) {
					count.start();
					if (approach == 1) {

						tv_voice_code.setText("您将会接到电话播报验证码，请保持手机畅通");
					}
				} else {
					if (jsonObject.optString("comment").equals("please login")) {
						Intent intent = new Intent(Reset_pwd_Mes.this,
								V6LoginActivity.class);
						intent.putExtra("close", 1);
						startActivity(intent);
					} else {
						Toast.makeText(Reset_pwd_Mes.this,
								jsonObject.optString("comment"), 2000).show();
					}
				}
				LogUtils.e(Reset_pwd_Mes.this, "重置交易密码"+jsonObject.toString());

			}

			@Override
			public void onError() {
				// TODO Auto-generated method stub

			}
		};
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setRequestMethod(0);
		volleryUtil.execute(Reset_pwd_Mes.this, Constant.URL
				+ "/api/v5/pass/mobileCaptcha?"
				, params.toString());

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_confirm:
			if (et_phone.getText().toString().equals("")) {
				Toast.makeText(Reset_pwd_Mes.this, "请输入手机号", 2000).show();
				return;
			}
			if (et_codes.getText().toString().equals("")) {
				Toast.makeText(Reset_pwd_Mes.this, "请输入短信验证码", 2000).show();
return;
			}
			next();
			break;
		case R.id.img_back:
			finish();
			break;
		case R.id.btn_send_mes:
			if (et_phone.getText().toString().equals("")) {
				Toast.makeText(Reset_pwd_Mes.this, "请输入手机号", 2000).show();

			} else {

				initmes(0);

			}
			break;
		case R.id.rl_back:
			finish();
			break;
		case R.id.enterprise_tel:
			BaseHelper.showDialog2(this, "拨号", "400-0101-016", 1);
			break;
		case R.id.tv_voice_code:
			if (et_phone.getText().toString().equals("")) {
				Toast.makeText(Reset_pwd_Mes.this, "请输入手机号", 2000).show();

			} else {

				initmes(1);

			}
			break;

		}

	}

	private void next() {
		// TODO Auto-generated method stub
		AjaxParams params = new AjaxParams();
		params.put("userMobile", et_phone.getText().toString());
		params.put("mobileCaptcha", et_codes.getText().toString());
		VolleryUtil volleryUtil = new VolleryUtil() {

			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.e("输出", jsonObject.toString());
				if (jsonObject.optInt("success") == 1) {

					Intent intent = new Intent(Reset_pwd_Mes.this,
							Reset_pwd.class);
					startActivity(intent);
					Reset_pwd_Mes.this.finish();
				}else {
					
					Toast.makeText(Reset_pwd_Mes.this,
							jsonObject.optString("comment"), 0).show();
				}
			}

			@Override
			public void onError() {
				// TODO Auto-generated method stub

			}
		};
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setRequestMethod(0);
		volleryUtil.setShow(true);
		volleryUtil.execute(Reset_pwd_Mes.this, Constant.URL
				+ "/api/v5/pass/step1?", params.toString());
	}

//	class TimeCount extends CountDownTimer {
//		public TimeCount(long millisInFuture, long countDownInterval) {
//			super(millisInFuture, countDownInterval);
//		}
//
//		@Override
//		public void onFinish() {// 计时完毕
//			btn_send_mes.setText("发送验证码");
//			btn_send_mes.setBackgroundResource(R.drawable.v6_captcha_shape);
//			btn_send_mes.setClickable(true);
//		}
//
//		@Override
//		public void onTick(long millisUntilFinished) {// 计时过程
//			btn_send_mes.setClickable(false);// 防止重复点击
//			btn_send_mes.setBackgroundResource(R.drawable.v6_unclick);
//			btn_send_mes.setText("已发送"+"("+millisUntilFinished / 1000+"秒)" );
//		}
//	}

}
